﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;

using CE.Promotion;
using CE.User;
using CE.Global;
using CE.Mail;
using CE.Image;
using JSON;
using Facebook_Graph_Toolkit;

public partial class promo_promo_facebook : SocialPage
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        ExtendedPermissions = "email,user_birthday,user_relationships, publish_stream";
        //Response.Write(Request.UserHostAddress.ToString());
        //RequireLogin = true;   Set to true if the page must access the user's Facebook info (i.e. Api is never null)
    }

    protected void FbAuthClick(object sender, EventArgs e)
    {
        if (Api == null) RedirectToFacebookAuthorization();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.Page.Culture = "en-US";

        System.Text.StringBuilder _strVariable = new System.Text.StringBuilder();
        _strVariable.Append("<table>");
        for (int i = 0; i < Request.ServerVariables.Count; i++)
        {
            _strVariable.Append("<tr><td>" + Request.ServerVariables.Keys[i].ToString() + "</td>");
            _strVariable.Append("<td>" + Request.ServerVariables[i].ToString() + "</td></tr>");
        }
        _strVariable.Append("</table>");
        lblServerVariables.Text = _strVariable.ToString();

        try
        {
            if (Api != null)
            {
                btnFacebookAuth.Enabled = false;
                divFaceBookDetails.Visible = true;
                Label_AccessToken.Text = Api.AccessToken;
                SocialPage _apage = new SocialPage();

                Label_AccessToken.Text = "";

                //string access_token = string.Empty;
                //WebClient _client = new WebClient();
                //access_token = _client.DownloadString("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=253027998061617&client_secret=a4505110eee4604ed3fb1ae350bbd07e");
                //access_token = access_token.Remove(0, 13);
                
                Facebook_Graph_Toolkit.GraphApi.User U = new Facebook_Graph_Toolkit.GraphApi.User("me", Api.AccessToken);
                if (U != null)
                {
                    Label_UserID.Text = U.ID;
                    Label_Name.Text = U.Name;
                    Label_FirstName.Text = U.FirstName;
                    Label_SurName.Text = U.LastName;
                    Label_Email.Text = U.Email;

                    if (U.Birthday.HasValue)
                    {
                        Label_DOB.Text = U.Birthday.Value.ToString("dd/MMMM/yyyy");
                    }
                    if (U.RelationshipStatus.HasValue) Label_RelationshipStatus.Text = U.RelationshipStatus.ToString();
                    btnPublisFBMessage.Enabled = true;
                }
            }
        }
        catch (FacebookException fex)
        {
            Response.Write("FEX:" + fex.Message);
            Session["GraphApi"] = null;
            Response.Redirect("promo-facebook.aspx");
            //Response.Redirect("https://www.facebook.com/dialog/oauth?client_id=253027998061617&redirect_uri=" + Server.UrlEncode(Request.Url.AbsoluteUri));
            //Facebook_Graph_Toolkit.GraphApi.AccessTokenValidationResult _accessToken = Facebook_Graph_Toolkit.GraphApi.Api.ValidateAccessToken(Api.AccessToken);
            //if (_accessToken != Facebook_Graph_Toolkit.GraphApi.AccessTokenValidationResult.Success)
            //{
                //Response.Redirect("https://graph.facebook.com/oauth/access_token?grant_type=client_credentials&client_id=253027998061617&client_secret=a4505110eee4604ed3fb1ae350bbd07e");
            //}
            
        }
        catch (Exception ex)
        {
            //Response.Write("EX:" + ex.Message);
            //Facebook_Graph_Toolkit.GraphApi.Api.ClearCache();
        }


        if (Session["ImageCount"] != null)
        {
            if (Convert.ToInt32(Session["ImageCount"].ToString()) > 5)
            {
                lblMessage.Text = "Please try after a few moment! You are session has been locked!";
                btnSendMail.Enabled = false;
                btnNewImage.Enabled = false;
                txtEmail.Enabled = false;
                txtSecurityCode.Enabled = false;
                //Response.Redirect("usermessage.aspx?mt=um&ec=1993");
            }
            else
            {
                lblMessage.Text = "";
                btnSendMail.Enabled = true;
                btnNewImage.Enabled = true;
                txtEmail.Enabled = true;
                txtSecurityCode.Enabled = true;
            }
        }

        if (!Page.IsPostBack)
        {
            txtEmailSubscribe.Text = "";

            if (!CheckIP(Request))
            {
                mpePopup.Show();
            }

            if (Session["ImageCount"] != null)
            {
                if (Convert.ToInt32(Session["ImageCount"].ToString()) <= 5)
                {
                    CreateUpdateImageAndSession();
                }
            }
            else
            {
                CreateUpdateImageAndSession();
            }


            Session[General.ViewedDetailsId] = LogUserViewedHistory().ToString();

            ShufflePromos();
            CheckTimer();
        }

    }

    protected void PublishClick(object sender, EventArgs e)
    {
        try
        {
            Facebook_Graph_Toolkit.FacebookObjects.LinkAttachment LA = new Facebook_Graph_Toolkit.FacebookObjects.LinkAttachment("CompareXpress Singapore", "http://www.comparexpress.com", "CompareXpress Promotion", "");
            Api.PostFeed("CompareXpress gets you cheap car insurance quotes that suits you in 2 minutes. Select the best car & motor insurance deals from trusted insurance companies and save $$$!", "", LA, null);
            btnPublisFBMessage.Text = "Message Published";
            btnPublisFBMessage.Enabled = false;
        }
        catch (Exception)
        {
            btnPublisFBMessage.Text = "An error has occured";
            btnPublisFBMessage.Enabled = false;
        }
    }

    private void CheckTimer()
    {
        DateTime dt = CE.Global.General.GetUTCDateTime();
        Int64 iDate = Convert.ToInt64(String.Format("{0:yMMddhhmm}", dt));


        //PROMO 1

        Int64 Promo1Start = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoStart1"));
        Int64 Promo1End = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd1"));
        string _Promo1End = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd1");

        if (iDate >= Promo1End)
        {
            UpdateTimer1.Enabled = false;
            divTimer1Days.Attributes.Add("class", "dash2 days_dash2");
            divTimer1Hours.Attributes.Add("class", "dash2 hours_dash2");
            divTimer1Minutes.Attributes.Add("class", "dash2 minutes_dash2");
            divTimer1Seconds.Attributes.Add("class", "dash2 seconds_dash2");

            divPromo1Disable.Attributes.Add("class", "darkClass");
        }

        //1111010000
        //if 1 day less remaining - change css
        DateTime dtPromo1End = Convert.ToDateTime(_Promo1End.Substring(2, 2) + "/" + _Promo1End.Substring(4, 2) + "/" + _Promo1End.Substring(0, 2));
        if (CE.Global.General.GetUTCDateTime() >= dtPromo1End.AddDays(-1) && CE.Global.General.GetUTCDateTime() < dtPromo1End)  //Promo Period  
        {
            divTimer1Days.Attributes.Add("class", "dash2 days_dash3");
            divTimer1Hours.Attributes.Add("class", "dash2 hours_dash3");
            divTimer1Minutes.Attributes.Add("class", "dash2 minutes_dash3");
            divTimer1Seconds.Attributes.Add("class", "dash2 seconds_dash3");
        }

        //END OF PROMO 1

        //PROMO 2

        Int64 Promo2Start = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoStart2"));
        Int64 Promo2End = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd2"));
        string _Promo2End = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd2");

        if (iDate >= Promo2End)
        {
            UpdateTimer2.Enabled = false;
            divTimer2Days.Attributes.Add("class", "dash2 days_dash2");
            divTimer2Hours.Attributes.Add("class", "dash2 hours_dash2");
            divTimer2Minutes.Attributes.Add("class", "dash2 minutes_dash2");
            divTimer2Seconds.Attributes.Add("class", "dash2 seconds_dash2");

            divPromo2Disable.Attributes.Add("class", "darkClass");
        }

        //if 1 day less remaining - change css
        DateTime dtPromo2End = Convert.ToDateTime(_Promo2End.Substring(2, 2) + "/" + _Promo2End.Substring(4, 2) + "/" + _Promo2End.Substring(0, 2));
        if (CE.Global.General.GetUTCDateTime() >= dtPromo2End.AddDays(-1) && CE.Global.General.GetUTCDateTime() < dtPromo2End)
        {
            divTimer2Days.Attributes.Add("class", "dash2 days_dash3");
            divTimer2Hours.Attributes.Add("class", "dash2 hours_dash3");
            divTimer2Minutes.Attributes.Add("class", "dash2 minutes_dash3");
            divTimer2Seconds.Attributes.Add("class", "dash2 seconds_dash3");
        }

        //END OF PROMO 2

        //PROMO 3

        Int64 Promo3Start = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoStart3"));
        Int64 Promo3End = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd3"));
        string _Promo3End = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd3");

        if (iDate >= Promo3End)
        {
            UpdateTimer3.Enabled = false;
            divTimer3Days.Attributes.Add("class", "dash2 days_dash2");
            divTimer3Hours.Attributes.Add("class", "dash2 hours_dash2");
            divTimer3Minutes.Attributes.Add("class", "dash2 minutes_dash2");
            divTimer3Seconds.Attributes.Add("class", "dash2 seconds_dash2");
        }

        //if 1 day less remaining - change css
        DateTime dtPromo3End = Convert.ToDateTime(_Promo3End.Substring(2, 2) + "/" + _Promo3End.Substring(4, 2) + "/" + _Promo3End.Substring(0, 2));
        if (CE.Global.General.GetUTCDateTime() >= dtPromo3End.AddDays(-1) && CE.Global.General.GetUTCDateTime() < dtPromo3End)
        {
            divTimer3Days.Attributes.Add("class", "dash2 days_dash3");
            divTimer3Hours.Attributes.Add("class", "dash2 hours_dash3");
            divTimer3Minutes.Attributes.Add("class", "dash2 minutes_dash3");
            divTimer3Seconds.Attributes.Add("class", "dash2 seconds_dash3");

            divPromo3Disable.Attributes.Add("class", "darkClass");
        }

        //END OF PROMO 3

        //PROMO 4

        Int64 Promo4Start = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoStart4"));
        Int64 Promo4End = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd4"));
        string _Promo4End = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd4");

        if (iDate >= Promo4End)
        {
            UpdateTimer4.Enabled = false;
            divTimer4Days.Attributes.Add("class", "dash2 days_dash2");
            divTimer4Hours.Attributes.Add("class", "dash2 hours_dash2");
            divTimer4Minutes.Attributes.Add("class", "dash2 minutes_dash2");
            divTimer4Seconds.Attributes.Add("class", "dash2 seconds_dash2");

            divPromo4Disable.Attributes.Add("class", "darkClass");
        }

        //if 1 day less remaining - change css
        DateTime dtPromo4End = Convert.ToDateTime(_Promo4End.Substring(2, 2) + "/" + _Promo4End.Substring(4, 2) + "/" + _Promo4End.Substring(0, 2));
        if (CE.Global.General.GetUTCDateTime() >= dtPromo4End.AddDays(-1) && CE.Global.General.GetUTCDateTime() < dtPromo4End)
        {
            divTimer4Days.Attributes.Add("class", "dash2 days_dash3");
            divTimer4Hours.Attributes.Add("class", "dash2 hours_dash3");
            divTimer4Minutes.Attributes.Add("class", "dash2 minutes_dash3");
            divTimer4Seconds.Attributes.Add("class", "dash2 seconds_dash3");
        }

        //END OF PROMO 4

        //PROMO 5

        Int64 Promo5Start = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoStart5"));
        Int64 Promo5End = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd5"));
        string _Promo5End = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd5");

        if (iDate >= Promo5End)
        {
            UpdateTimer5.Enabled = false;
            divTimer5Days.Attributes.Add("class", "dash2 days_dash2");
            divTimer5Hours.Attributes.Add("class", "dash2 hours_dash2");
            divTimer5Minutes.Attributes.Add("class", "dash2 minutes_dash2");
            divTimer5Seconds.Attributes.Add("class", "dash2 seconds_dash2");

            divPromo5Disable.Attributes.Add("class", "darkClass");
        }

        //if 1 day less remaining - change css
        DateTime dtPromo5End = Convert.ToDateTime(_Promo5End.Substring(2, 2) + "/" + _Promo5End.Substring(4, 2) + "/" + _Promo5End.Substring(0, 2));
        if (CE.Global.General.GetUTCDateTime() >= dtPromo5End.AddDays(-1) && CE.Global.General.GetUTCDateTime() < dtPromo5End)
        {
            divTimer5Days.Attributes.Add("class", "dash2 days_dash3");
            divTimer5Hours.Attributes.Add("class", "dash2 hours_dash3");
            divTimer5Minutes.Attributes.Add("class", "dash2 minutes_dash3");
            divTimer5Seconds.Attributes.Add("class", "dash2 seconds_dash3");
        }

        //END OF PROMO 5

        //PROMO 6

        Int64 Promo6Start = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoStart6"));
        Int64 Promo6End = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd6"));
        string _Promo6End = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd6");

        if (iDate >= Promo6End)
        {
            UpdateTimer6.Enabled = false;
            divTimer6Days.Attributes.Add("class", "dash2 days_dash2");
            divTimer6Hours.Attributes.Add("class", "dash2 hours_dash2");
            divTimer6Minutes.Attributes.Add("class", "dash2 minutes_dash2");
            divTimer6Seconds.Attributes.Add("class", "dash2 seconds_dash2");

            divPromo6Disable.Attributes.Add("class", "darkClass");
        }

        //if 1 day less remaining - change css
        DateTime dtPromo6End = Convert.ToDateTime(_Promo6End.Substring(2, 2) + "/" + _Promo6End.Substring(4, 2) + "/" + _Promo6End.Substring(0, 2));
        if (CE.Global.General.GetUTCDateTime() >= dtPromo6End.AddDays(-1) && CE.Global.General.GetUTCDateTime() < dtPromo6End)
        {
            divTimer6Days.Attributes.Add("class", "dash2 days_dash3");
            divTimer6Hours.Attributes.Add("class", "dash2 hours_dash3");
            divTimer6Minutes.Attributes.Add("class", "dash2 minutes_dash3");
            divTimer6Seconds.Attributes.Add("class", "dash2 seconds_dash3");
        }

        //END OF PROMO 6

        //PROMO 7

        Int64 Promo7Start = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoStart7"));
        Int64 Promo7End = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd7"));
        string _Promo7End = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd7");

        if (iDate >= Promo7End)
        {
            UpdateTimer7.Enabled = false;
            divTimer7Days.Attributes.Add("class", "dash2 days_dash2");
            divTimer7Hours.Attributes.Add("class", "dash2 hours_dash2");
            divTimer7Minutes.Attributes.Add("class", "dash2 minutes_dash2");
            divTimer7Seconds.Attributes.Add("class", "dash2 seconds_dash2");

            divPromo7Disable.Attributes.Add("class", "darkClass");
        }

        //if 1 day less remaining - change css
        DateTime dtPromo7End = Convert.ToDateTime(_Promo7End.Substring(2, 2) + "/" + _Promo7End.Substring(4, 2) + "/" + _Promo7End.Substring(0, 2));
        if (CE.Global.General.GetUTCDateTime() >= dtPromo7End.AddDays(-1) && CE.Global.General.GetUTCDateTime() < dtPromo7End)
        {
            divTimer7Days.Attributes.Add("class", "dash2 days_dash3");
            divTimer7Hours.Attributes.Add("class", "dash2 hours_dash3");
            divTimer7Minutes.Attributes.Add("class", "dash2 minutes_dash3");
            divTimer7Seconds.Attributes.Add("class", "dash2 seconds_dash3");
        }

        //END OF PROMO 7

        //PROMO 8

        Int64 Promo8Start = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoStart8"));
        Int64 Promo8End = Convert.ToInt64(CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd8"));
        string _Promo8End = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd8");

        if (iDate >= Promo8End)
        {
            UpdateTimer8.Enabled = false;
            divTimer8Days.Attributes.Add("class", "dash2 days_dash2");
            divTimer8Hours.Attributes.Add("class", "dash2 hours_dash2");
            divTimer8Minutes.Attributes.Add("class", "dash2 minutes_dash2");
            divTimer8Seconds.Attributes.Add("class", "dash2 seconds_dash2");

            divPromo8Disable.Attributes.Add("class", "darkClass");
        }

        //if 1 day less remaining - change css
        DateTime dtPromo8End = Convert.ToDateTime(_Promo8End.Substring(2, 2) + "/" + _Promo8End.Substring(4, 2) + "/" + _Promo8End.Substring(0, 2));
        if (CE.Global.General.GetUTCDateTime() >= dtPromo8End.AddDays(-1) && CE.Global.General.GetUTCDateTime() < dtPromo8End)
        {
            divTimer8Days.Attributes.Add("class", "dash2 days_dash3");
            divTimer8Hours.Attributes.Add("class", "dash2 hours_dash3");
            divTimer8Minutes.Attributes.Add("class", "dash2 minutes_dash3");
            divTimer8Seconds.Attributes.Add("class", "dash2 seconds_dash3");
        }

        //END OF PROMO 8
    }

    private void ShufflePromos()
    {
        string[] cssPos = new string[8];
        cssPos[0] = "promogrid1";
        cssPos[1] = "promogrid2";
        cssPos[2] = "promogrid3";
        cssPos[3] = "promogrid4";
        cssPos[4] = "promogrid5";
        cssPos[5] = "promogrid6";
        cssPos[6] = "promogrid7";
        cssPos[7] = "promogrid8";

        Random parts = new Random();

        inputPromoPosBase.Value = "\n";
        for (int i = cssPos.Length - 1; i > 0; i--)
        {
            int j = parts.Next(i + 1);
            Swap(ref cssPos[i], ref cssPos[j]);

            // display in a random order
            inputPromoPosBase.Value += cssPos[i] + "\n";
        }

        divPromo1.Attributes.Add("class", cssPos[0]);
        divPromo2.Attributes.Add("class", cssPos[1]);
        divPromo3.Attributes.Add("class", cssPos[2]);
        divPromo4.Attributes.Add("class", cssPos[3]);
        divPromo5.Attributes.Add("class", cssPos[4]);
        divPromo6.Attributes.Add("class", cssPos[5]);
        divPromo7.Attributes.Add("class", cssPos[6]);
        divPromo8.Attributes.Add("class", cssPos[7]);

    }

    private void Swap(ref string firstElement, ref string secondElement)
    {
        string temp = firstElement;
        firstElement = secondElement;
        secondElement = temp;
    }

    private bool CheckIP(HttpRequest ClientRequest)
    {
        bool isExisting = false;

        CXPromoView cxpromo = new CXPromoView();
        CXPromoViewDB cxpromodb = new CXPromoViewDB();

        string IPnumber = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();
        //ClientRequest.UserHostAddress;

        cxpromo = cxpromodb.GetSingleItemByIP(IPnumber);

        if (cxpromo != null)
        {
            txtEmailFrom.Text = cxpromo.EmailAddress;
            txtEmailFrom.ReadOnly = true;
            isExisting = true;

        }
        else
        {
            txtEmailFrom.ReadOnly = false;
        }

        return isExisting;

    }

    private void SaveUserDetails(HttpRequest ClientRequest)
    {
        if (!CheckIP(ClientRequest))
        {
            CXPromoView cxpromoNew = new CXPromoView();
            CXPromoViewDB cxpromodbNew = new CXPromoViewDB();

            string IPnumber = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName()).AddressList.GetValue(0).ToString();

            cxpromoNew.PromoViewId = 0;
            cxpromoNew.IPNumber = IPnumber;// ClientRequest.UserHostAddress;
            cxpromoNew.BrowserType = ClientRequest.Browser.Type;
            cxpromoNew.EmailAddress = txtEmail.Text;

            txtEmailFrom.Text = txtEmail.Text;
            txtEmailFrom.ReadOnly = true;

            cxpromodbNew.Insert(cxpromoNew);
        }
    }

    protected void btnGo_Click(object sender, EventArgs e)
    {
        SaveUserDetails(Request);

    }

    private Int64 LogUserViewedHistory()
    {
        UserViewHistoryDB _userviewdb = new UserViewHistoryDB();
        return _userviewdb.LogResultViewedHistory("CXPR", Request, "Promo Page");
    }

    protected void UpdateTimer1_Tick(object sender, EventArgs e)
    {

        //TIMER BEFORE PROMO END
        string PromoEnd = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd1");
        //year/month/days/minutes/seconds -yy/mm/dd/mm/ss
        string _year = PromoEnd.Substring(0, 2);
        string _month = PromoEnd.Substring(2, 2);
        string _day = PromoEnd.Substring(4, 2);

        //DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year + " 9:52 AM");
        DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year);
        DateTime dtNow = CE.Global.General.GetUTCDateTime();

        if (dtNow <= dtEnd)
        {
            TimeSpan ts = dtEnd - dtNow;

            lblTimer1Days.Text = string.Format("{0:00}", ts.Days); //ts.Days.ToString();
            lblTimer1Hours.Text = string.Format("{0:00}", ts.Hours);
            lblTimer1Minutes.Text = string.Format("{0:00}", ts.Minutes);
            lblTimer1Seconds.Text = string.Format("{0:00}", ts.Seconds);
        }
        else
        {
            lblTimer1Days.Text = "00";
            lblTimer1Hours.Text = "00";
            lblTimer1Minutes.Text = "00";
            lblTimer1Seconds.Text = "00";

            UpdateTimer1.Enabled = false;
            //Response.Redirect("promo.aspx");
        }


    }

    protected void UpdateTimer2_Tick(object sender, EventArgs e)
    {

        //TIMER BEFORE PROMOTION END
        string PromoEnd = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd2");
        //year/month/days/minutes/seconds -yy/mm/dd/mm/ss
        string _year = PromoEnd.Substring(0, 2);
        string _month = PromoEnd.Substring(2, 2);
        string _day = PromoEnd.Substring(4, 2);

        //DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year + " 9:52 AM");
        DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year);
        DateTime dtNow = CE.Global.General.GetUTCDateTime();

        if (dtNow <= dtEnd)
        {
            TimeSpan ts = dtEnd - dtNow;

            lblTimer2Days.Text = string.Format("{0:00}", ts.Days); //ts.Days.ToString();
            lblTimer2Hours.Text = string.Format("{0:00}", ts.Hours);
            lblTimer2Minutes.Text = string.Format("{0:00}", ts.Minutes);
            lblTimer2Seconds.Text = string.Format("{0:00}", ts.Seconds);
        }
        else
        {
            lblTimer2Days.Text = "00";
            lblTimer2Hours.Text = "00";
            lblTimer2Minutes.Text = "00";
            lblTimer2Seconds.Text = "00";

            UpdateTimer2.Enabled = false;
        }


    }

    protected void UpdateTimer3_Tick(object sender, EventArgs e)
    {

        //TIMER BEFORE PROMOTION END
        string PromoEnd = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd3");
        //year/month/days/minutes/seconds -yy/mm/dd/mm/ss
        string _year = PromoEnd.Substring(0, 2);
        string _month = PromoEnd.Substring(2, 2);
        string _day = PromoEnd.Substring(4, 2);

        //DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year + " 9:52 AM");
        DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year);
        DateTime dtNow = CE.Global.General.GetUTCDateTime();

        if (dtNow <= dtEnd)
        {
            TimeSpan ts = dtEnd - dtNow;

            lblTimer3Days.Text = string.Format("{0:00}", ts.Days); //ts.Days.ToString();
            lblTimer3Hours.Text = string.Format("{0:00}", ts.Hours);
            lblTimer3Minutes.Text = string.Format("{0:00}", ts.Minutes);
            lblTimer3Seconds.Text = string.Format("{0:00}", ts.Seconds);
        }
        else
        {
            lblTimer3Days.Text = "00";
            lblTimer3Hours.Text = "00";
            lblTimer3Minutes.Text = "00";
            lblTimer3Seconds.Text = "00";

            UpdateTimer3.Enabled = false;
        }


    }

    protected void UpdateTimer4_Tick(object sender, EventArgs e)
    {

        //TIMER BEFORE PROMOTION END
        string PromoEnd = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd4");
        //year/month/days/minutes/seconds -yy/mm/dd/mm/ss
        string _year = PromoEnd.Substring(0, 2);
        string _month = PromoEnd.Substring(2, 2);
        string _day = PromoEnd.Substring(4, 2);

        //DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year + " 9:52 AM");
        DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year);
        DateTime dtNow = CE.Global.General.GetUTCDateTime();

        if (dtNow <= dtEnd)
        {
            TimeSpan ts = dtEnd - dtNow;

            lblTimer4Days.Text = string.Format("{0:00}", ts.Days); //ts.Days.ToString();
            lblTimer4Hours.Text = string.Format("{0:00}", ts.Hours);
            lblTimer4Minutes.Text = string.Format("{0:00}", ts.Minutes);
            lblTimer4Seconds.Text = string.Format("{0:00}", ts.Seconds);
        }
        else
        {
            lblTimer4Days.Text = "00";
            lblTimer4Hours.Text = "00";
            lblTimer4Minutes.Text = "00";
            lblTimer4Seconds.Text = "00";

            UpdateTimer4.Enabled = false;
        }


    }

    protected void UpdateTimer5_Tick(object sender, EventArgs e)
    {

        //TIMER BEFORE PROMOTION END
        string PromoEnd = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd5");
        //year/month/days/minutes/seconds -yy/mm/dd/mm/ss
        string _year = PromoEnd.Substring(0, 2);
        string _month = PromoEnd.Substring(2, 2);
        string _day = PromoEnd.Substring(4, 2);

        //DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year + " 9:52 AM");
        DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year);
        DateTime dtNow = CE.Global.General.GetUTCDateTime();

        if (dtNow <= dtEnd)
        {
            TimeSpan ts = dtEnd - dtNow;

            lblTimer5Days.Text = string.Format("{0:00}", ts.Days); //ts.Days.ToString();
            lblTimer5Hours.Text = string.Format("{0:00}", ts.Hours);
            lblTimer5Minutes.Text = string.Format("{0:00}", ts.Minutes);
            lblTimer5Seconds.Text = string.Format("{0:00}", ts.Seconds);
        }
        else
        {
            lblTimer5Days.Text = "00";
            lblTimer5Hours.Text = "00";
            lblTimer5Minutes.Text = "00";
            lblTimer5Seconds.Text = "00";

            UpdateTimer5.Enabled = false;
        }


    }

    protected void UpdateTimer6_Tick(object sender, EventArgs e)
    {

        //TIMER BEFORE PROMOTION END
        string PromoEnd = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd6");
        //year/month/days/minutes/seconds -yy/mm/dd/mm/ss
        string _year = PromoEnd.Substring(0, 2);
        string _month = PromoEnd.Substring(2, 2);
        string _day = PromoEnd.Substring(4, 2);

        //DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year + " 9:52 AM");
        DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year);
        DateTime dtNow = CE.Global.General.GetUTCDateTime();

        if (dtNow <= dtEnd)
        {
            TimeSpan ts = dtEnd - dtNow;

            lblTimer6Days.Text = string.Format("{0:00}", ts.Days); //ts.Days.ToString();
            lblTimer6Hours.Text = string.Format("{0:00}", ts.Hours);
            lblTimer6Minutes.Text = string.Format("{0:00}", ts.Minutes);
            lblTimer6Seconds.Text = string.Format("{0:00}", ts.Seconds);
        }
        else
        {
            lblTimer6Days.Text = "00";
            lblTimer6Hours.Text = "00";
            lblTimer6Minutes.Text = "00";
            lblTimer6Seconds.Text = "00";

            UpdateTimer6.Enabled = false;
        }


    }

    protected void UpdateTimer7_Tick(object sender, EventArgs e)
    {

        //TIMER BEFORE PROMOTION END
        string PromoEnd = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd7");
        //year/month/days/minutes/seconds -yy/mm/dd/mm/ss
        string _year = PromoEnd.Substring(0, 2);
        string _month = PromoEnd.Substring(2, 2);
        string _day = PromoEnd.Substring(4, 2);

        //DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year + " 9:52 AM");
        DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year);
        DateTime dtNow = CE.Global.General.GetUTCDateTime();

        if (dtNow <= dtEnd)
        {
            TimeSpan ts = dtEnd - dtNow;

            lblTimer7Days.Text = string.Format("{0:00}", ts.Days); //ts.Days.ToString();
            lblTimer7Hours.Text = string.Format("{0:00}", ts.Hours);
            lblTimer7Minutes.Text = string.Format("{0:00}", ts.Minutes);
            lblTimer7Seconds.Text = string.Format("{0:00}", ts.Seconds);
        }
        else
        {
            lblTimer7Days.Text = "00";
            lblTimer7Hours.Text = "00";
            lblTimer7Minutes.Text = "00";
            lblTimer7Seconds.Text = "00";

            UpdateTimer7.Enabled = false;
        }


    }

    protected void UpdateTimer8_Tick(object sender, EventArgs e)
    {

        //TIMER BEFORE PROMOTION END
        string PromoEnd = CE.Configuration.MyConfigurationManager.GetAppSettingValue("CXPromoEnd8");
        //year/month/days/minutes/seconds -yy/mm/dd/mm/ss
        string _year = PromoEnd.Substring(0, 2);
        string _month = PromoEnd.Substring(2, 2);
        string _day = PromoEnd.Substring(4, 2);

        //DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year + " 9:52 AM");
        DateTime dtEnd = Convert.ToDateTime(_month + "/" + _day + "/20" + _year);
        DateTime dtNow = CE.Global.General.GetUTCDateTime();

        if (dtNow <= dtEnd)
        {
            TimeSpan ts = dtEnd - dtNow;

            lblTimer8Days.Text = string.Format("{0:00}", ts.Days); //ts.Days.ToString();
            lblTimer8Hours.Text = string.Format("{0:00}", ts.Hours);
            lblTimer8Minutes.Text = string.Format("{0:00}", ts.Minutes);
            lblTimer8Seconds.Text = string.Format("{0:00}", ts.Seconds);
        }
        else
        {
            lblTimer8Days.Text = "00";
            lblTimer8Hours.Text = "00";
            lblTimer8Minutes.Text = "00";
            lblTimer8Seconds.Text = "00";

            UpdateTimer8.Enabled = false;
        }


    }

    protected void btnSendMail_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            lblMessage.Text = "";

            if (Session[CE.Global.General.SecurityCheckWord] != null)
            {
                string _usrText = txtSecurityCode.Text.ToUpper();
                string _imgText = Session[CE.Global.General.SecurityCheckWord].ToString().ToUpper();

                if (_usrText == _imgText)
                {
                    if (SendMailToUser())
                    {
                        lblMessage.Text = "Email has been sent to your friend.";
                        CreateUpdateImageAndSession();
                        //TAUserOptionLog(false, false, true);
                        //Response.Redirect("usermessage.aspx");
                    }
                }
                else
                {
                    lblMessage.Text = "Wrong security check!";
                    CreateUpdateImageAndSession();
                    mpeSendMail.Show();
                }
            }
            else
            {
                lblMessage.Text = "Security Check code was expired. Please try again.";
                CreateUpdateImageAndSession();
                mpeSendMail.Show();
            }
        }
    }

    private bool SendMailToUser()
    {

        string userfirstname = txtFullName.Text;

        bool _retVal = false;

        string _emailTo = txtFriendEmail.Text;
        string _customername = userfirstname;
        string _emailSubject = "<CompareXpress> Travel Insurance Promo";
        string _mailBody = "";
        string _adLocation = CE.Configuration.MyConfigurationManager.GetAppSettingValue("TravelPromoEDM"); // "http://www.comparexpress.co/edm/ace/NATAS_CX.jpg";

        _mailBody = "<body class='email_body'>";
        _mailBody += "<center>";
        _mailBody += "hi, your friend " + userfirstname + " would like you to check out this promotion. <br/>";
        _mailBody += "<div>";
        _mailBody += "<img src='" + _adLocation + "' alt='CompareXpress' border='0' />";
        _mailBody += "</div><br/>";
        _mailBody += "<hr />";
        _mailBody += "<br /><i>This is a computer-generated email. Please do not reply to this email.</i>";
        _mailBody += "<hr />";
        _mailBody += "</center></body>";

        if (MyMailManager.SendCRMMessage(_emailTo, _emailSubject, _mailBody))
        {
            _retVal = true;
        }
        else
        {
            _retVal = false;
        }

        return _retVal;
    }

    protected void btnNewImage_Click(object sender, EventArgs e)
    {
        CreateUpdateImageAndSession();
    }

    private void CreateUpdateImageAndSession()
    {
        MyImage myimage = new MyImage();
        string salt = "";

        if (Session[CE.Global.General.SecurityCheckWord] != null)
        {
            salt = myimage.GetRandomWord(Session[CE.Global.General.SecurityCheckWord].ToString());
        }
        else
        {
            if (Session[CE.Global.General.SecurityCheckWord] != null)
            {
                salt = myimage.GetRandomWord(Session[CE.Global.General.SecurityCheckWord].ToString());
            }
            else
            {
                salt = myimage.GetRandomWord("all");
            }
        }

        Session[CE.Global.General.SecurityCheckWord] = salt;
        imgCAPTCHA.ImageUrl = @"~/CAPTCHA/CAPTCHAImageHandler.ashx?id=" + CE.Security.MySecurity.Encrypt(salt, true);
        txtSecurityCode.Text = "";
    }

    protected void btnSubscribe_Click(object sender, EventArgs e)
    {
        if (Page.IsValid)
        {
            UserSubscriber _usersub = new UserSubscriber();
            UserSubscriberDB _usersubdb = new UserSubscriberDB();
            _usersub.Email = txtEmailSubscribe.Text;
            if (_usersubdb.Insert(_usersub) > 0)
            {
                divSuccess.Visible = true;
                txtEmailSubscribe.Enabled = false;
                btnSubscribe.Enabled = false;
            }
        }
    }

    protected void CheckIsExisting(object sender, ServerValidateEventArgs args)
    {
        UserSubscriber _usersub = new UserSubscriber();
        UserSubscriberDB _usersubdb = new UserSubscriberDB();
        _usersub.Email = txtEmailSubscribe.Text;
        if (_usersubdb.AlreadyExist(_usersub.Email) > 0)
        {
            //lblMessageSubscribe.Text = "Your email is already existing in our list!";
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }

    }

}
